import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import Login from "../views/Login.vue";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "login",
    component: Login,
  },

  {
    path: "/home",
    name: "home",
    component: HomeView,
    redirect: "/home/Kzt",
    children: [
      {
        path: "/home/ChangePass",
        component: () => import("@/views/ChangePass.vue"),
      },
      //控制台
      {
        path: "/home/Kzt",
        component: () => import("@/views/KZT/Kzt.vue"),
      },
      //装备管理
      // {
      //   path: "/home/Zbfl",
      //   component: () => import("@/views/ZBGL/Zbfl.vue"),
      // },
      // {
      //   path: "/home/Zbgl",
      //   component: () => import("@/views/ZBGL/Zbgl.vue"),
      // },
      {
        path: "/home/ZbglAdd",
        component: () => import("@/views/ZBGL/Zbgl/ZbglAdd.vue"),
      },
      {
        path:'/home/ZbglEdit',
        component:()=>import('@/views/ZBGL/Zbgl/ZbglEdit.vue'),
      },
      {
        path:'/home/ZbglSee',
        component:()=>import('@/views/ZBGL/Zbgl/ZbglSee.vue'),
        redirect:'/home/ZbglSee/Qcqd',
        children:[
          {
            path: "/home/ZbglSee/Qcqd",
            component: () => import("@/views/ZBGL/Zbgl/ZbglSee/Qcqd.vue"),
          },
          {
            path: "/home/ZbglSee/Wxjl",
            component: () => import("@/views/ZBGL/Zbgl/ZbglSee/Wxjl.vue"),
          },
          {
            path: "/home/ZbglSee/Jszl",
            component: () => import("@/views/ZBGL/Zbgl/ZbglSee/Jszl.vue"),
          },
        ],
      },
      // 仓库管理
      // {
      //   path: "/home/Bmll",
      //   component: () => import("@/views/CKGL/Bmll.vue"),
      // },
      // {
      //   path: "/home/Bmtl",
      //   component: () => import("@/views/CKGL/Bmtl.vue"),
      // },
      //退料新增
      {
        path: "/home/BmtlAdd",
        component: () => import("@/views/CKGL/BMTL/BmtlAdd.vue"),
      },
      //退料编辑
      {
        path: "/home/Bmtledit",
        component: () => import("@/views/CKGL/BMTL/Bmtledit.vue"),
      },
      // {
      //   path: "/home/Hwxx",
      //   component: () => import("@/views/CKGL/Hwxx.vue"),
      // },
      // {
      //   path: "/home/Kcpd",
      //   component: () => import("@/views/CKGL/Kcpd.vue"),
      // },
      //库存盘点新增
      {
        path: "/home/KcpdAdd",
        component: () => import("@/views/CKGL/BMTL/KcpdAdd.vue"),
      },
      //库存盘点编辑
      {
        path: "/home/Kcpdedit",
        component: () => import("@/views/CKGL/BMTL/Kcpdedit.vue"),
      },
      // 器材管理添加信息路由
      {
        path: "/Qcgl/Qcgl_xz",
        component: () => import("@/views/CKGL/Qcgl_xz/Qcgl_xz.vue"),
      },
      // 器材管理修改信息
      {
        path: "/Qcgl/Qcgl_xg",
        component: () => import("@/views/CKGL/Qcgl_xz/Qcgl_xg.vue"),
      },
      // {
      //   path: "/home/Qcfl",
      //   component: () => import("@/views/CKGL/Qcfl.vue"),
      // },
      // {
      //   path: "/home/Qcgl",
      //   component: () => import("@/views/CKGL/Qcgl.vue"),
      // },
      // {
      //   path: "/home/Rkgl",
      //   component: () => import("@/views/CKGL/Rkgl.vue"),
      // },
      //新增入库
      {
        path: "/home/AddRkd",
        component: () => import("@/views/CKGL/AddRkd.vue"),
      },
      // 维修管理
      // {
      //   path: "/home/Wxda",
      //   component: () => import("@/views/WXGL/Wxda.vue"),
      // },
      // {
      //   path: "/home/Wxjl",
      //   component: () => import("@/views/WXGL/Wxjl.vue"),
      // },
      // 维修记录新增
      {
        path: "/home/Wxjl_add",
        component: () => import("@/views/WXGL/Wxjl_view/Wxjl_add.vue"),
      },
      // {
      //   path: "/home/Wxjf",
      //   component: () => import("@/views/WXGL/Wxjf.vue"),
      // },
      // 查看维修记录详情
      {
        path: "/home/Wxjl_view",
        component: () => import("@/views/WXGL/Wxjl_view/Wxjl_view.vue"),
      },
      // {
      //   path: "/home/Wxjh",
      //   component: () => import("@/views/WXGL/Wxjh.vue"),
      // },
      {
        path: '/home/WxjhEdit',
        component: () => import('@/views/WXGL/Wxjh/WxjhEdit.vue'),
      },
      {
        path: '/home/Zlxq',
        component: () => import('@/views/WXGL/Wxzl/Zlxq.vue'),
      },
      //质量详情
      {
        path: "/home/WxjhAdd",
        component: () => import("@/views/WXGL/Wxjh/WxjhAdd.vue"),
      },
      // {
      //   path: "/home/Wxzl",
      //   component: () => import("@/views/WXGL/Wxzl.vue"),
      // },
      //维修质量新增
      {
        path:'/home/WxzlAdd',
        component:()=>import('@/views/WXGL/Wxzl/Wxzlxz.vue'),
      },
      {
        path: "/home/Jfxq",
        component: () => import("@/views/WXGL/Jfxq.vue"),
      },
      {
        path: "/home/Daxq",
        component: () => import("@/views/WXGL/Daxq.vue"),
      },
      //人员管理
      // {
      //   path: "/home/Kqgl",
      //   component: () => import("@/views/RYGL/Kqgl.vue"),
      // },
      // {
      //   path: "/home/Ryxx",
      //   component: () => import("@/views/RYGL/Ryxx.vue"),
      // },
      // {
      //   path: "/home/Wxgd",
      //   component: () => import("@/views/RYGL/Wxgd.vue"),
      // },
      {
        path: "/home/Rycz",
        component: () => import("@/views/RYGL/Rycz.vue"),
      },
      {
        path: "/home/Tjry",
        component: () => import("@/views/RYGL/Tjry.vue"),
      },
      //数据统计
      {
        path: "/home/Fxltj",
        component: () => import("@/views/SJTJ/Fxltj.vue"),
      },
      {
        path: "/home/Qcdyph",
        component: () => import("@/views/SJTJ/Qcdyph.vue"),
      },
      {
        path: "/home/Wxjftj",
        component: () => import("@/views/SJTJ/Wxjftj.vue"),
      },
      {
        path: "/home/Wxltj",
        component: () => import("@/views/SJTJ/Wxltj.vue"),
      },
      {
        path: "/home/Wxryjx",
        component: () => import("@/views/SJTJ/Wxryjx.vue"),
      },
      {
        path: "/home/Wxxjx",
        component: () => import("@/views/SJTJ/Wxxjx.vue"),
      },
      {
        path: "/home/Zbgzph",
        component: () => import("@/views/SJTJ/Zbgzph.vue"),
      },
      //技术资料
      // {
      //   path: "/home/Zlgl",
      //   component: () => import("@/views/JSZL/Zlgl.vue"),
      // },
      // 资料管理添加资料
      {
        path: "/home/Zlgl_add",
        component: () => import("@/views/JSZL/Zlgl_/Zlgl_add.vue"),
      },
      // 资料管理资料详情
      {
        path: "/home/Zlgl_detail",
        component: () => import("@/views/JSZL/Zlgl_/Zlgl_detail.vue"),
      },
      // 修改资料管理信息
      {
        path: "/home/Zlgl_edit",
        component: () => import("@/views/JSZL/Zlgl_/Zlgl_edit.vue"),
      },
      // {
      //   path: "/home/Zllx",
      //   component: () => import("@/views/JSZL/Zllx.vue"),
      // },
      //规章制度
      // {
      //   path: "/home/Fggl",
      //   component: () => import("@/views/GZZD/Fggl.vue"),
      // },
      //添加法规
      {
        path: "/home/Fgtj",
        component: () => import("@/views/GZZD/Fggl/Fgtj.vue"),
      },
      //法规编辑
      {
        path: "/home/Fgbj",
        component: () => import("@/views/GZZD/Fggl/Fgbj.vue"),
      },
      // {
      //   path: "/home/Fglx",
      //   component: () => import("@/views/GZZD/Fglx.vue"),
      // },
      //系统管理
      // {
      //   path: "/home/Jsgl",
      //   component: () => import("@/views/XTSZ/Jsgl.vue"),
      // },
      // {
      //   path: "/home/Xtsz",
      //   component: () => import("@/views/XTSZ/Xtsz.vue"),
      // },
      // {
      //   path: "/home/Yhgl",
      //   component: () => import("@/views/XTSZ/Yhgl.vue"),
      // },
      {
        path: "/home/tjyh",
        component: () => import("@/views/XTSZ/Tjyh.vue"),
      },
      {
        path: "/home/tjjs",
        component: () => import("@/views/XTSZ/Tjjs.vue"),
      },
      {
        path: "/home/xzlld",
        component: () => import("@/views/CKGL/Xzlld.vue"),
      },
      {
        path: "/home/cklld",
        component: () => import("@/views/CKGL/Cklld.vue"),
      },
    ],
  },
];

// 全局路由守备
// 解决问题：刷新页面到错误页面的问题
/* router.beforeEach((to, from,next) => {
  // 获取路由组件数量
  console.log("to:",to)
  console.log("from:",from)
  console.log("路由节点",router.getRoutes())
  if(to.path == '/' || to.path == '/login'){
    console.log('去登录页面')
    next()
  }else{
    console.log('去非登录页面')
    let power = sessionStorage.getItem('power')
    if(power){
      // 已经登录
      console.log( '登录成功')
      let routerArr = router.getRoutes()
      if(routerArr.length > 6){
        // 创建过路由
        next()
      }else{
        // 没有创建路由
        console.log('刷新页面')
        // 再次创建动态路由
        JSON.parse(power).forEach((v)=>{
          router.addRoute('Home',{path:v.href,component:() => import (`../views/${v.com}.vue`)})
        })
        // 
        console.log({...to})
        next({...to,replace:true})
      }
    }else{
      // 没有登录
      next({path:'/',replace:true})
    }
  }
});
 */

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from,next) => {
  if(to.path=='/'){
    next();
  }else{
    let routing=JSON.parse(sessionStorage.getItem('user'))[0].routing;
    if(routing){
      let routerArr=router.getRoutes();
      // console.log(router.getRoutes())
      if(routerArr.length>43){
        next()
      }else{
        routing.forEach((v) => {
          for(let i=0;i<v.children.length;i++){
            router.addRoute('home',{
              path:v.children[i].href,
              component:()=>import(`@/views/${v.children[i].com}.vue`)
            })
          }
        });
        next({...to,replace:true})
      }
    }else{
      next({path:'/',replace:true})
    }
  }
})

export default router;
